﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;

namespace mesRUpload
{
    public enum LoggerName
    {
        MesRUplodLogger = 0x0001,
        MiFindLogger,
        SmtpLogger,
        DebugLogger,
        ErrorLogger,
        InfoLogger
    }

    public class Logger
    {
        private static Logger m_instance;

        private static readonly ILog log = LogManager.GetLogger("DebugAppender");
        
        private log4net.ILog logger;

        private static Dictionary<LoggerName, log4net.ILog> logerContainer;

        public static Logger getInstance()
        {

            if (logerContainer == null)
            {
                logerContainer = new Dictionary<LoggerName, ILog>();
            }

            if (logerContainer.Count > 0)
            {
                logerContainer.Clear();
            }

            logerContainer.Add(LoggerName.MesRUplodLogger, log4net.LogManager.GetLogger("MesRUplodLogger"));
            logerContainer.Add(LoggerName.MiFindLogger, log4net.LogManager.GetLogger("MiFindLogger"));
            logerContainer.Add(LoggerName.SmtpLogger, log4net.LogManager.GetLogger("SmtpLogger"));
            logerContainer.Add(LoggerName.DebugLogger, log4net.LogManager.GetLogger("DebugLogger"));
            logerContainer.Add(LoggerName.ErrorLogger, log4net.LogManager.GetLogger("ErrorLogger"));
            logerContainer.Add(LoggerName.InfoLogger, log4net.LogManager.GetLogger("InfoLogger"));

            if (m_instance == null)
            {
                m_instance = new Logger();
                Console.WriteLine("初始化日志配置完成");
            }

            log.Info(""); // 表头

            return m_instance;
        }

        private ILog setLoggerName(LoggerName name)
        {
            log4net.ILog log;
            bool bRet = logerContainer.TryGetValue(name, out log);
            return log;

            //string appenderName = "DebugAppender";
            //switch (name)
            //{
            //    case LoggerName.MesRUplodLogger:
            //        appenderName = "MesRUplodAppender";
            //        break;
            //    case LoggerName.MiFindLogger:
            //        appenderName = "MiFindAppender";
            //        break;
            //    case LoggerName.SmtpLogger:
            //        appenderName = "SmtpAppender";
            //        break;
            //    case LoggerName.DebugLogger:
            //        appenderName = "DebugAppender";
            //        break;
            //    case LoggerName.ErrorLogger:
            //        appenderName = "ErrorAppender";
            //        break;
            //    case LoggerName.InfoLogger:
            //        appenderName = "InfoAppender";
            //        break;
            //    default:
            //        break;
            //}

            //try
            //{
            //    var repository = LogManager.GetRepository();
            //    var appenders = repository.GetAppenders();
            //    var targetAppender = appenders.First(p => p.Name == appenderName) as RollingFileAppender;
                
            //}
            //catch (Exception e)
            //{
            //    this.Fatal(e.Message);
            //    return log;
            //}

        }

        public void Info(LoggerName name, object message)
        {
            if (log.IsInfoEnabled)
            {
                setLoggerName(name).Info(message);
            }
        }

        public void Error(LoggerName name, object message)
        {
            if (log.IsErrorEnabled )
                setLoggerName(name).Error(message);
        }

        public void Fatal(LoggerName name, object message)
        {
            if (log.IsFatalEnabled)
                setLoggerName(name).Fatal(message);
        }

        //public void Warn(LoggerName name, object message)
        //{
        //    if (setLoggerName(LoggerName name, out logger) && log.IsWarnEnabled)
        //        setLoggerName(name)..Warn(message);
        //}

        //public void Debug(LoggerName name, object message)
        //{
        //    if (setLoggerName(LoggerName name, out logger) && log.IsDebugEnabled)
        //        log.Debug(message);
        //}

        //public void Debug(LoggerName name, object message, Exception exception)
        //{
        //    if (setLoggerName(LoggerName name, out logger) && log.IsDebugEnabled)
        //        log.Debug(message, exception);
        //}

        //public void Error(LoggerName name, object message, Exception exception)
        //{
        //    if (log.IsInfoEnabled)
        //        setLoggerName(name).Error(message, exception);
        //}

        public string exceptionMess(Exception e)
        {
            return "{\"Exception\":[\"Message\":\"" + e.Message.ToString() + "\""
                //+ ",\"Source\":\"" + e.Source.ToString() + "\""
                //+ ",\"TargetSite\":\"" + e.TargetSite.ToString() + "\""
                //+ ",\"StackTrace\":\"" + e.StackTrace.ToString() + "\""
                    + "]}";
        }


        public void Info(object message)
        {
            if (log.IsInfoEnabled)
                log.Info(message);
        }

        public void Error(object message)
        {
            if (log.IsErrorEnabled)
                log.Error(message);
        }

        public void Fatal(object message)
        {
            if (log.IsFatalEnabled)
                log.Fatal(message);
        }

        public void Warn(object message)
        {
            if (log.IsWarnEnabled)
                log.Warn(message);
        }

        public void Debug(object message)
        {
            if (log.IsDebugEnabled)
                log.Debug(message);
        }

        public void Debug(object message, Exception exception)
        {
            if (log.IsDebugEnabled)
                log.Debug(message, exception);
        }

        public void Error(object message, Exception exception)
        {
            if (log.IsInfoEnabled)
                log.Error(message, exception);
        }

    }
}